<?php

require_once( '../../conf/conf.inc' );
require_once( PATH_FRAME.'/etwork.php' );

$etw = new etwork;
$etw->parseURL('edit/category', 2);

$etw->isEdit = true;
$etw->limitLevel = access::MASTER;

if (!isset($etw->pathParameters[0])) {

    //カテゴリトップ
    $etw->setTemplate('category', TRUE, TRUE, 0);

    $etw->setPankuzu(array(
        array('dir'=>'', 'config'=>'index'),
        array('dir'=>'edit', 'config'=>'modelate')
    ));
    $etw->setTitle(array('category','list'));


    $loader = new modelLoader;
    $loader->model  = 'category';
    $loader->order  = @$etw->EX_GET['r'] ? $etw->EX_GET['r'] : '_update_date';
    $loader->alike  = @$etw->EX_GET['s'];
    $loader->offset = @$etw->EX_GET['p'];
    $loader->returning = 'list';
    $etw->addLoader($loader);
    
    $etw->loadData();

} else if (isNumber($etw->pathParameters[0])) {
    
    $etw->setTemplate('category', TRUE, false);
    $etw->addClearTemplate( null, 'category');

    $etw->setPankuzu(array(
        array('dir'=>'', 'config'=>'index'),
        array('dir'=>'edit', 'config'=>'modelate'),
        array('dir'=>'edit/category', 'config'=>array('category','list'))
    ));
    if ($etw->pathParameters[0] > 0)
        $etw->setTitle(array('edit'),array('solo'=>'name'));
    else
        $etw->setTitle(array('category','newpost'));

    $etw->jumpTo = URL_SITE .'edit/category';

    $loader = new modelLoader;
    $loader->model = 'category';
    $loader->targetId = 'category_id';
    $loader->isEdit = true;
    $loader->solo = $etw->pathParameters[0];
    $etw->addLoader($loader);
    
    $etw->loadData();
    
} else {

    if (! isset($etw->pathParameters[1])) {

        $etw->setTemplate('child', TRUE, TRUE, 0);
        $etw->setTemplateGroup('category');

        $etw->setPankuzu(array(
            array('dir'=>'', 'config'=>'index'),
            array('dir'=>'edit', 'config'=>'modelate'),
            array('dir'=>'edit/category', 'config'=>array('category','list'))
        ));
        $etw->setTitle(array('category','element','list'),array('parent'=>'name'));

        $loader = new modelLoader;
        $loader->model  = 'category';
        $loader->order  = @$etw->EX_GET['r'] ? $etw->EX_GET['r'] : '_update_date';
        $loader->alike  = @$etw->EX_GET['s'];
        $loader->offset = @$etw->EX_GET['p'];
        $loader->mapperParameters = array(
            'slug' => $etw->pathParameters[0]
        );
        $loader->targetSelectFunction .= 'child';
        $loader->returning = 'list';

        $etw->addLoader($loader);
    
    } else if (isNumber($etw->pathParameters[1])) {
        
        $etw->setTemplate('child', TRUE, false);
        $etw->setTemplateGroup('category');
        $etw->addClearTemplate( null, 'category|'.$etw->pathParameters[0]);

        $etw->setPankuzu(array(
            array('dir'=>'', 'config'=>'index'),
            array('dir'=>'edit', 'config'=>'modelate'),
            array('dir'=>'edit/category', 'config'=>array('category','list')),
            array('dir'=>'edit/category/'.$etw->pathParameters[0], 'config'=>array('category','element','list'), 'load'=>array('parent'=>'name'))
        ));
        if ($etw->pathParameters[1] > 0)
            $etw->setTitle(array('edit'),array('solo'=>'name'));
        else
            $etw->setTitle(array('category','element','newpost'));


        $etw->jumpTo = URL_SITE . 'edit/category/' . $etw->pathParameters[0];
    
        $loader = new modelLoader;
        $loader->model = 'category';
        $loader->targetChoiceFunction = 'findChildById';
        $loader->targetInsertFunction .= 'Child';
        $loader->targetUpdateFunction .= 'Child';
        $loader->targetDeleteFunction .= 'Child';
        $loader->targetId = 'category_list_id';
        $loader->isEdit = true;
        $loader->solo = $etw->pathParameters[1];
        
        $etw->addLoader($loader);
    }

    $loader = new modelLoader;
    $loader->model = 'category';
    $loader->targetId = 'slug';
    $loader->solo = $etw->pathParameters[0];
    $loader->targetChoiceFunction = 'findBySlug';
    $loader->returning = 'parent';

    $etw->addLoader($loader);
    
    $etw->loadData();
}

$etw->showFinal();


